package com.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ems.entity.StationEntity;
import com.ems.mapper.AlarmMapper;
import com.ems.mapper.StationMapper;
import com.ems.service.IStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.AlarmVo;
import com.ems.vo.StationVo;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 储能电站 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-10-24
 */
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, StationEntity> implements IStationService {

    @Resource
    private StationMapper stationMapper;
    @Resource
    private AlarmMapper alarmMapper;

    @Override
    public List<StationVo> queryList (StationVo stationVo) {
        stationVo.setPage ( ( stationVo.getPage () - 1 ) * stationVo.getLimit () );
        List<StationVo> stationVos = stationMapper.queryList ( stationVo );
        return stationVos;
    }

    @Override
    public Integer queryCount (StationVo stationVo) {
        Integer count = stationMapper.queryCount ( stationVo );
        return count;
    }

    @Override
    public StationVo saveSingle (StationVo stationVo) {
        if ( stationVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        StationEntity stationEntity = new StationEntity ();
        BeanUtils.copyProperties ( stationVo, stationEntity );
        stationEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        stationEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( stationEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return stationVo;
    }

    @Override
    public StationVo querySingle (StationVo stationVo) {
        QueryWrapper<StationEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( stationVo.getId () ) ) {
            queryWrapper.eq ( "id", stationVo.getId () );
        }
        StationEntity stationEntity = baseMapper.selectOne ( queryWrapper );
        StationVo vo = new StationVo ();
        BeanUtils.copyProperties ( stationEntity, vo );
        return vo;
    }

    @Override
    public StationVo updateSingle (StationVo stationVo) {
        if ( stationVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( stationVo.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        UpdateWrapper<StationEntity> updateWrapper = new UpdateWrapper<> ();
        if ( StringUtils.isNotBlank ( stationVo.getStationName () ) ) {
            updateWrapper.set ( "stationName", stationVo.getStationName () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getPower () ) ) {
            updateWrapper.set ( "power", stationVo.getPower () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getCover () ) ) {
            updateWrapper.set ( "cover", stationVo.getCover () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getProvinceId () ) ) {
            updateWrapper.set ( "provinceId", stationVo.getProvinceId () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getCityId () ) ) {
            updateWrapper.set ( "cityId", stationVo.getCityId () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getCountyId () ) ) {
            updateWrapper.set ( "countyId", stationVo.getCountyId () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getAddress () ) ) {
            updateWrapper.set ( "address", stationVo.getAddress () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getPlace () ) ) {
            updateWrapper.set ( "place", stationVo.getPlace () );
        }
        if ( stationVo.getEmsType () != null ) {
            updateWrapper.set ( "emsType", stationVo.getEmsType () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getLongitude () ) ) {
            updateWrapper.set ( "longitude", stationVo.getLongitude () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getLatitude () ) ) {
            updateWrapper.set ( "latitude", stationVo.getLatitude () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getCapacity () ) ) {
            updateWrapper.set ( "capacity", stationVo.getCapacity () );
        }
        if ( StringUtils.isNotBlank ( stationVo.getMerchantId () ) ) {
            updateWrapper.set ( "merchantId", stationVo.getMerchantId () );
        }
        if ( stationVo.getStartTime () != null ) {
            updateWrapper.set ( "startTime", stationVo.getStartTime () );
        }
        if ( stationVo.getIsUsed () != null ) {
            updateWrapper.set ( "isUsed", stationVo.getIsUsed () );
        }
        updateWrapper.eq ( "id", stationVo.getId () );
        updateWrapper.set ( "changeTime", new Date () );
        int update = baseMapper.update ( null, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return stationVo;
    }

    @Override
    public List<StationVo> getStationList (StationVo stationVo) {
        List<StationEntity> stationEntities = baseMapper.selectList ( new QueryWrapper<> () );
        List<StationVo> stationVos = new ArrayList<> ();
        for ( StationEntity stationEntity : stationEntities ) {
            StationVo vo = new StationVo ();
            BeanUtils.copyProperties ( stationEntity, vo );
            stationVos.add ( vo );
        }
        return stationVos;
    }

    /**
     * 单站概况和所有电站列表
     * 1、所有电站列表
     * 2、对应电站的基本信息
     * @param stationVo
     * @return
     */
    @Override
    public Map<String, Object> singleSite (StationVo stationVo) {
        Map<String, Object> map = new HashMap<> ();
        List<StationEntity> stationEntities = baseMapper.selectList ( new QueryWrapper<> () );
        map.put ( "stationEntities", stationEntities );
        // 如果有指定电站Id，则返回指定电站信息，否则返回第一个电站信息
        if ( StringUtils.isNotBlank ( stationVo.getId () ) ) {
            StationEntity stationEntity = baseMapper.selectById ( stationVo );
            StationVo vo = new StationVo ();
            BeanUtils.copyProperties ( stationEntity, vo );
            map.put ( "singleSite", vo );
        }
        map.put ( "singleSite", stationEntities.get ( 0 ) );
        return map;
    }

    @Override
    public List<StationVo> queryStationAlarmList (StationVo stationVo) {
        stationVo.setPage ( ( stationVo.getPage () - 1 ) * stationVo.getLimit () );
        List<StationVo> stationVos = stationMapper.queryList ( stationVo );
        for ( StationVo vo : stationVos ) {
            // 查询每个电站下所有报警列表
            AlarmVo alarmVo = new AlarmVo ();
            alarmVo.setStationId ( vo.getId () );
            List<AlarmVo> alarmVos = alarmMapper.queryTodayList ( alarmVo );
            vo.setAlarmNumber ( alarmVos.size () );
            int count = (int) alarmVos.stream ().filter ( alarmEndEr -> alarmEndEr.getStatus () == 1 ).count ();
            vo.setAlarmOccurring ( count );
            vo.setAlarmEndEr ( alarmVos.size () - count );
        }
        return stationVos;
    }

}
